<template lang="pug">
.article(v-loading="loading")
  .article-header
    .article-title 发表记录
    el-dropdown(trigger="click")
      el-button(name="primary" size="small") 发布
      el-dropdown-menu(slot="dropdown")
        el-dropdown-item
          router-link(tag="a" target="_blank" :to="{ name: 'editArticle' }") 发布图文文章
        el-dropdown-item
          router-link(tag="a" target="_blank" :to="{ name: 'editVideo' }") 发布视频文章
  .article-filter
    .article-filter-tabs
      .tab-item(v-for="item, index in tabs", :class="type == item.value ? 'active' : ''", :key="index" @click="changeTab(item)") {{item.label}}
    .article-filter-search
      el-input(prefix-icon="el-icon-search" v-model="searchContent" clearable placeholder="输入标题/关键词" @input="changeSearch")
  .article-container
    .article-content(v-if="list.length")
      .article-section(v-for="item, index in list" :key="index")
        .article-time {{item.groupDate}}
        .article-list
          .article-item(v-for="child, childIndex in item.list" :key="child.articleId" @click.stop="preview(child)")
            .article-item-hover
              .article-handle(@click.stop="edit(child)")
                i.iconfont.icon-edit-1
              .article-handle(@click.stop="deleteArticle(child)")
                i.el-icon-delete
            .article-item-type {{child.typeStr}}
            .article-item-bd
              .article-item-media.img(:style="`background-image: url(${child.coverImageUrl})`")
              .article-item-info
                .article-item-title {{child.title}}
                .article-item-nums
                  .article-item-num
                    i.iconfont.icon-like
                    .article-item-num-bd {{child.likeCount}}
                  .article-item-num
                    i.iconfont.icon-eye
                    .article-item-num-bd {{child.viewCount}}
    .article-empty(v-else)
      img(src="../../static/images/empty_.png")
      .article-empty-txt 暂无数据...
  .article-footer(v-if="list.length")
    el-pagination(
      background
      layout="prev, pager, next, jumper"
      :total="total"
      :page-size="pageSize"
      :current-page="currentPage"
      @current-change="currentChange"
    )
</template>

<script>
import { getArticleList, postDelArticle } from 'api'
export default {
  name: 'articleManage',
  data () {
    return {
      tabs: [
        { label: '全部', value: '' },
        { label: '视频', value: 2 },
        { label: '图文', value: 1 }
      ],
      list: [],
      searchContent: '',
      type: '',
      loading: false,
      pageSize: 20,
      currentPage: 1,
      total: 0
    }
  },
  methods: {
    changeTab (item) {
      this.currentPage = 1
      this.type = item.value
      this.getList()
    },
    changeSearch (value) {
      this.getList()
    },
    currentChange (page) {
      this.currentPage = page
      this.getList()
    },
    preview (item) {
      let name = item.type === 2 ? 'previewVideo' : 'previewArticle'
      let routeUrl = this.$router.resolve({
        name: name,
        params: { id: item.articleId }
      })
      window.open(routeUrl.href, '_blank')
    },
    edit (item) {
      let name = item.type === 2 ? 'editVideo' : 'editArticle'
      let routeUrl = this.$router.resolve({
        name: name,
        params: { id: item.articleId }
      })
      window.open(routeUrl.href, '_blank')
    },
    deleteArticle (item) {
      this.$confirm('确定删除该文章？删除后用户将无法访问该文章内容。', '提示').then(async () => {
        await this.$axios.post(postDelArticle, { articleId: item.articleId })
        this.$message.success('删除成功！')
        this.getList()
      })
    },
    async getList () {
      try {
        this.loading = true
        let query = {
          currentPage: this.currentPage,
          pageSize: this.pageSize,
          draft: false,
          isGroupByDate: true
        }
        if (this.searchContent) query.searchContent = this.searchContent
        if (this.type) query.type = this.type
        const { ResultSet } = await this.$axios.post(getArticleList, { ...query })
        this.list = ResultSet.userData.data || []
        this.total = ResultSet.userData.total
        this.loading = false
      } catch (err) {
        this.loading = false
        this.$message.error(err.message || err.ResultSet.errors)
      }
    }
  },
  beforeRouteEnter (to, from, next) {
    next(vm => {
      vm.getList()
    })
  }
}
</script>

<style lang="scss" scoped>
.article {
  width: 100%;
  height: 100%;
  // padding: 0 40px;
}
.article-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 15px 40px;
}
.article-title {
  font-size: 16px;
  color: #363636;
  line-height: 22px;
}
.article-filter {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 40px;
}
.article-filter-tabs {
  display: flex;
  align-items: center;
  padding-left: 10px
}
.tab-item {
  font-size: 14px;
  line-height: 22px;
  color: #363636;
  margin-right: 50px;
  cursor: pointer;
  &.active {
    color: #ED5B75;
  }
}
.article-filter-search {
  .el-input,
  .el-input__inner {
    background: #f5f5f5 !important;
  }
}
.article-container {
  height: calc(100% - 186px);
  padding: 0 40px;
  overflow: auto;
}
.article-content {
  margin-top: 15px;
}
.article-section {
  background: #fff;
  border-radius: 4px;
  margin-bottom: 15px;
}
.article-time {
  padding: 25px 32px;
  font-weight: 700;
  font-size: 14px;
  line-height: 20px;
  border-bottom: 1px solid #DCDFE6;
}
.article-list {
  padding: 22px 32px;
}
.article-item {
  position: relative;
  padding: 20px 32px;
  display: flex;
  &:hover {
    .article-item-hover {
      display: flex;
    }
  }
}
.article-item-hover {
  display: none;
  position: absolute;
  align-items: center;
  justify-content: flex-end;
  padding: 0 50px;
  top: 0;
  left: 0;
  bottom: 0;
  right: 0;
  background: rgba(0, 0, 0, 0.5);
  border-radius: 4px;
}
.article-handle {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  background: #fff;
  border: 1px solid #DCDFE6;
  margin-left: 25px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  .iconfont {
    font-size: 16px;
    color: #999;
  }
}
.article-item-type {
  font-size: 14px;
  line-height: 20px;
  color: #3E4373;
}
.article-item-bd {
  flex: 1;
  padding-left: 40px;
  display: flex;
}
.article-item-media {
  width: 135px;
  height: 90px;
  border-radius: 6px;
  &.img {
    background-position: center;
    background-repeat: no-repeat;
    background-size: cover;
  }
}
.article-item-info {
  display: flex;
  flex: 1;
  flex-direction: column;
  justify-content: space-between;
  padding-left: 10px;
  .article-item-title {
    max-width: 420px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    font-size: 14px;
    line-height: 16px;
    color: #3E4373;
  }
  .article-item-nums {
    display: flex;
    align-items: center;
  }
}
.article-item-num {
  display: flex;
  align-items: center;
  color: #999;
  margin-right: 25px;
  .iconfont {
    font-size: 14px;
  }
  .article-item-num-bd {
    font-size: 12px;
    margin-left: 3px;
  }
}
.article-empty {
  padding-top: 80px;
  text-align: center;
}
.article-empty-txt {
  font-size: 16px;
  margin-top: 10px;
  color: #999;
}
.article-footer {
  padding: 20px 40px;
  text-align: right;
}
</style>